package com.AdvancedDemos.FileAndIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class 练习2 {
    public static void main(String[] args) {
        /**
         * 生成10个[1,9]之间的随机数,存储到当前模块下的source.txt文件中,格式如下:
         * 2 3 7 5 6 8 9 1 2 4
         * 读取该数据,将数据排序后,重新存储到当前模块下的result.txt文件中
         */
        File file = new File("F:\\IntelijIDEA\\IntelliJ IDEA 2022.3\\IdeaProjects\\MyFirstProject\\helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\source.txt");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            for (int i = 0, temp; i < 10; i++) {
                temp = (int) (Math.random() * 9) + 1;
                fileOutputStream.write((Integer.toString(temp) + " ").getBytes());
                System.out.print(temp + " ");
            }
            System.out.println("\n写入source.txt完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        /*读取该数据,将数据排序后*/
        String result = null;//用于存储从文件中读取的内容
        FileInputStream fileInputStream = null;
        //TreeSet自带升序排序
        TreeSet<Integer> integers = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //必须手动修改比较规则,不然会自动去重,导致排序后数据不全
                return o1 > o2 ? 1 : -1;
            }
        });
        try {
            if (file.length() >= Integer.MAX_VALUE) {//防止文件过大
                throw new RuntimeException("文件过大!");
            }
            fileInputStream = new FileInputStream(file);
            byte[] bytes = fileInputStream.readAllBytes();
            //读取全部字节,放到字节数组中;注意:最大读取容量是Integer.MAX_VALUE
            result = new String(bytes);//将字符数组转成字符串
            String[] stringNumbers = result.split(" ");//去空格,拆成字符串数组
            for (int i = 0; i < stringNumbers.length; i++) {
                Integer integer = Integer.valueOf(stringNumbers[i]);
                integers.add(integer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("读取source.txt完成,排序完成");
        System.out.println(integers);

        /*重新存储到当前模块下的result.txt文件中*/
        fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("F:\\IntelijIDEA\\IntelliJ IDEA 2022.3\\IdeaProjects\\MyFirstProject\\helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\result.txt");
            Iterator<Integer> iterator = integers.iterator();
            while (iterator.hasNext()) {
                Integer next = iterator.next();
                fileOutputStream.write((Integer.toString(next) + " ").getBytes());
                System.out.print(next + " ");
            }
            System.out.println("\n写入result.txt完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
